package com.cxyzy.demo

import android.app.AlertDialog
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private lateinit var alertDialog: AlertDialog;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        showSimpleDialogBtn.setOnClickListener { showSimpleDialog() }
        showComplexDialogBtn.setOnClickListener { showComplexDialog() }
    }

    /**
     * dialog基本用法
     */
    private fun showSimpleDialog() {
        val message = "我是dialog内容"
        val alertDialog = AlertDialog.Builder(this).setMessage(message).setCancelable(false)
            .setPositiveButton(android.R.string.ok)
            { _, _ ->
                Toast.makeText(this@MainActivity, "用户点击了确定", Toast.LENGTH_SHORT).show()
            }
            .setNegativeButton(android.R.string.cancel) { _, _ ->
                Toast.makeText(this@MainActivity, "用户点击了取消", Toast.LENGTH_SHORT).show()
            }
            .create()
        alertDialog.show()
    }

    /**
     * dialog完整用法
     */
    private fun showComplexDialog() {
        val message = "我是dialog内容"
        alertDialog = AlertDialog.Builder(this).setMessage(message).setCancelable(false)
            .setIcon(R.mipmap.ic_launcher)
            .setTitle("标题")
            .setPositiveButton(android.R.string.ok)
            { _, _ ->
                Toast.makeText(this@MainActivity, "用户点击了确定", Toast.LENGTH_SHORT).show()
            }
            .setNegativeButton(android.R.string.cancel) { _, _ ->
                Toast.makeText(this@MainActivity, "用户点击了取消", Toast.LENGTH_SHORT).show()
            }
            .setCancelable(false)//默认就是false，如果希望支持返回键和点击对话框外消失就设置为true
            .create()
        alertDialog.show()
    }



}
